using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Entities;
namespace DAL
{
  public  class tbDealDAL
    {
             private SqlConnection cn = null;
      public tbDealDAL()
      {
          cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
          cn.Open();
      }
      public void Insert(tbDeal d)
      {
          SqlCommand cm = new SqlCommand("Deal_Insert", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Masp", d.Masp );
          cm.Parameters.AddWithValue("@Tinhtrang", d.Tinhtrang );
          cm.ExecuteNonQuery();
      }
      public void Update(tbDeal d)
      {
          SqlCommand cm = new SqlCommand("Deal_Update", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Madeal", d.Madeal);
          cm.Parameters.AddWithValue("@Masp", d.Masp);
          cm.Parameters.AddWithValue("@Tinhtrang",d.Tinhtrang );
          cm.ExecuteNonQuery();

      }
      public void Delete(int iD)
      {
          SqlCommand cm = new SqlCommand("Deal_Delete", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Madeal", iD);
          cm.ExecuteNonQuery();
      }
      public tbDealCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
      { 
          SqlCommand cm = new SqlCommand("Deal_Get", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
         tbDealCollection    dCollection = new tbDealCollection  ();
         while (dr.Read())
         {
             tbDeal   d= new tbDeal ();
           d.Madeal  = int.Parse(dr.GetValue(0).ToString());
            d.Masp = int.Parse(dr.GetValue(1).ToString());
            d.Tinhtrang = Boolean.Parse(dr.GetValue(2).ToString());
             dCollection.Add(d);
         }
         dr.Close();
         totalRecords = int.Parse(
         cm.Parameters["@TotalRecords"].Value.ToString());
         return dCollection ;
      }
  

    }
}
